﻿#include "comm_impl.h"
#include <QAbstractSocket>

UsrCommImpl* UsrCommImpl::Instance()
{
    static UsrCommImpl slot;

    return &slot;
}

UsrCommImpl::UsrCommImpl()
{
    qRegisterMetaType<UsrCommImpl::logType>("comm logType");
    qRegisterMetaType<QAbstractSocket::SocketState>("QAbstractSocket::SocketState");
}


void UsrCommImpl::CommSlotLog(enum logType type, const QString& title, const QString& log)
{
    QString msg = "";

    switch(type)
    {
    case logSTR:
        msg = QString("%1:%2").arg(title).arg(log);
        break;
    case logDateTime:
        msg = QString("[%1]%2:%3").arg(MyDATETIMEMS).arg(title).arg(log);
        break;
    default: break;
    }

    if(!msg.isEmpty()) emit CommSignalLog(msg);
}

QString UsrCommImpl::byteArrayToHexStr(const QByteArray &data)
{
    QString temp = "";
    QString hex = data.toHex();

    for (int i = 0; i < hex.length(); i = i + 2) {
        temp += hex.mid(i, 2) + " ";
    }

    return temp.trimmed().toUpper();
}
